package com.king.juc.n8.jucdemo.countdownlatch;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.king.juc.utils.ThreadUtil.sleep;

/**
 * CountDownLatch 基本使用
 *
 * @author king
 * @date 2020/12/13 17:09
 */
@Slf4j(topic = "c.CountDownLatchTest")
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
        CountDownLatch latch = new CountDownLatch(10);
        Random r = new Random();
        String[] all = new String[10];
        for (int j = 0; j < 10; j++) {
            int k = j;
            service.submit(() -> {
                for (int i = 0; i <= 100; i++) {
                    try {
                        Thread.sleep(r.nextInt(100));
                    } catch (InterruptedException e) {
                        log.error("error: ", e);
                    }
                    all[k] = i + "%";

                    // 不换行， \r 替换前面放的内容
                    System.out.print("\r" + Arrays.toString(all));
                }
                latch.countDown();
            });
        }
        latch.await();
        System.out.println("\n游戏开始");
        service.shutdown();
    }

    /**
     * 线程池基本使用
     */
    private static void test2() {
        ExecutorService server = Executors.newFixedThreadPool(4);
        CountDownLatch latch = new CountDownLatch(3);
        server.submit(() -> {
            log.debug("beging...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        server.submit(() -> {
            log.debug("beging...");
            sleep(3);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        server.submit(() -> {
            log.debug("beging...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        });
        server.submit(() -> {
            log.debug("waiting...");
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("end...");
        });
    }

    /**
     * CountDownLatch基本使用
     * @throws InterruptedException
     */
    private static void test1() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);

        new Thread(() -> {
            log.debug("beging...");
            sleep(1);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        }).start();

        new Thread(() -> {
            log.debug("beging...");
            sleep(3);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        }).start();

        new Thread(() -> {
            log.debug("beging...");
            sleep(2);
            latch.countDown();
            log.debug("end...{}", latch.getCount());
        }).start();

        log.debug("waiting...");
        latch.await();
        log.debug("end...");
    }
}
